package com.zys.el.user.controller;


import com.zys.el.core.po.ResponseBean;
import com.zys.el.user.config.po.Resources;
import com.zys.el.user.po.*;
import com.zys.el.user.service.*;
import com.zys.el.user.util.FPGrowth.Myfptree2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@CrossOrigin
@RestController
@RequestMapping(value = "/resources")
public class ResourcesController {

    @Autowired
    private ITargetService targetService;
    @Autowired
    private IExpertService expertService;
    @Autowired
    private IBookpdfService bookpdfService;
    @Autowired
    private IVideoUrlService videoUrlService;
    @Autowired
    private ITargetUserService targetUserService;
    @Autowired
    private ITaskService taskService;
    @Autowired
    private IFieldService fieldService;
    @Autowired
    private IFieldMenuService fieldMenuService;
    @Autowired
    private Myfptree2 fp;

    @GetMapping("/get-resources/{id}")
    public ResponseBean getResources(@PathVariable("id") Long userId) {

        Resources resources = new Resources();
        ResponseBean rb = new ResponseBean();
        List<FieldMenu> fieldMenuByFieldId = new ArrayList<>();//存储得到的资源id

        if (userId != null) {
            TargetUser targetUser = targetUserService.selectTargetByUserId(userId);
//            System.out.println("targetUser" + targetUser);
            if (targetUser.equals(null) == false) {
                Target target = targetService.getById(targetUser.getTargetId());

                resources.setTarget(target);
                if (target != null) {
                    //用户id查到对应的target名字
                    String name = target.getName();
                    //根据target名字查出所有的target目标id列表
                    List<Target> targetByName = targetService.getTargetByName(name);
                    if (targetByName != null) {
                        //根据目标id列表一个个查出所有的对应的任务分解存到一个List<List<String>>中
                        List<List<String>> fpList = new ArrayList<>();
                        for (int i = 0; i < targetByName.size(); i++) {
                            List<Task> taskByTargetId = taskService.getTaskByTargetId(targetByName.get(i).getId());
                            if (taskByTargetId.equals(null) == false) {
                                fpList.add(new ArrayList<>());
                                for (Task task : taskByTargetId) {
                                    fpList.get(i).add(task.getName());
                                }
                            }
                            fpList.get(i).add(name);
                        }
                        //采用算法得到一个推荐任务列表
                        List<Task> ownTask = taskService.getTaskByTargetId(target.getId());
                        List<String> ownT = new ArrayList<>();
                        for (Task task : ownTask) {
                            ownT.add(task.getName());
                        }

                        Set<String> strings = fp.FPTreeLL(fpList, ownT);
//                        System.out.println(strings);
                        //根据任务列表一个一个地模糊搜索出多个领域列表然后拼接得到一个领域id列表
                        List<Field> fields = new ArrayList<>();
                        if (strings.equals(null) == false) {
                            for (String string : strings) {
                                fields.addAll(fieldService.QueryFieldByLikeName(string));
                            }
                        }
                        //根据领域id列表查出父id
                        if (fields.equals(null) == false) {
                            for (Field field : fields) {
                                fieldMenuByFieldId.addAll(fieldMenuService.getFieldMenuByFieldId(field.getId()));
                            }
                        }

                    }
                }
            }
        }

        //根据父id查出需要的资源（重复）
        if (fieldMenuByFieldId.equals(null) == false) {
            Set<Expert> experts = new HashSet<>();
            Set<BookPdf> bookPdfs = new HashSet<>();
            Set<VideoUrl> videoUrls = new HashSet<>();
//            System.out.println(fieldMenuByFieldId.size());
            for (FieldMenu fieldMenu : fieldMenuByFieldId) {
                Expert expert = expertService.getById(fieldMenu.getParentId());

                BookPdf bookPdf = bookpdfService.getById(fieldMenu.getParentId());

                VideoUrl videoUrl = videoUrlService.getById(fieldMenu.getParentId());

                if (expert != null) {
                    experts.add(expert);

                }
                if (bookPdfs != null) {
                    bookPdfs.add(bookPdf);
                }
                if (videoUrls != null) {
                    videoUrls.add(videoUrl);
                }
                expert = null;
                bookPdf = null;
                videoUrl = null;
            }

            experts.remove(null);
            bookPdfs.remove(null);
            videoUrls.remove(null);
            resources.setExperts(experts);
            resources.setBookPdfs(bookPdfs);
            resources.setVideoUrls(videoUrls);
        }
        rb.setModel(resources);
        return rb;
    }
}
